home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.04 Apr 95 / TreeAppƒ / Eric's C++ Libraries / Interface Classes / CPPControl.cp < prev    next >
Encoding:
Text File  |  1996-04-04  |  7.3 KB  |  281 lines  |  [TEXT/KAHL]

  1. /********************************************************* DEFINITION
  2.     DATE:    9/23/93
  3.     AUTHOR: Eric R. Rosé
  4.     
  5.     CLASS:  CPPControl
  6.     
  7.     SUPERCLASS: CPPVisualObject
  8.     
  9.         This C++ class manages a Button control
  10.     
  11. ********************************************************************/
  12.  
  13. #include <CPPControl.h>
  14. #include <CPPWindow.h>
  15.  
  16. extern Rect kDefaultRect;
  17. extern Rect kEmptyRect;
  18.  
  19. /*-----------------------------------------------------------------*/
  20. /*------------------------ PUBLIC METHODS -------------------------*/
  21. /*-----------------------------------------------------------------*/
  22.  
  23.     CPPControl::CPPControl (CPPWindow *itsWindow, Boolean canBeTarget,
  24.                             Boolean active, Boolean visible) :
  25.                 CPPVisualObject (itsWindow, &kDefaultRect, canBeTarget, 
  26.                                  active, visible)
  27.     /* called in case a subclass wants to construct the control itself */
  28.     {
  29.         this->theControl = NULL;
  30.         this->hasFrame = FALSE;
  31.         this->isEnabled = TRUE;
  32.         this->myValue = 1;
  33.         this->callBackProc = NULL;
  34.     }
  35.  
  36. /*-----------------------------------------------------------------*/
  37.  
  38.     CPPControl::CPPControl (CPPWindow *itsWindow, short ResID,
  39.                               Boolean isFramed,
  40.                              Boolean canBeTarget,
  41.                              Boolean active, Boolean visible) :
  42.                 CPPVisualObject (itsWindow, &kDefaultRect, canBeTarget, 
  43.                                  active, visible)
  44.     /* load a control resource and initialize with the given data */
  45.     {
  46.         
  47.         if (this->owningWindow)
  48.           {
  49.             this->theControl = GetNewControl (ResID, this->owningWindow);
  50.             this->hasFrame = isFramed;
  51.             this->isEnabled = TRUE;
  52.             this->myValue = 0;
  53.           }
  54.         this->callBackProc = NULL;
  55.     }
  56.  
  57. /*-----------------------------------------------------------------*/
  58.  
  59.     CPPControl::CPPControl (CPPWindow *itsWindow, Rect *itsBounds,
  60.                              short ControlType, StringPtr itsText,
  61.                               Boolean isFramed,
  62.                               Boolean useWindowFont,
  63.                              Boolean canBeTarget,
  64.                              Boolean active, Boolean visible) :
  65.                 CPPVisualObject (itsWindow, itsBounds, canBeTarget, 
  66.                                  active, visible)
  67.     {
  68.         short    procid = (useWindowFont) ? ControlType | 8 : ControlType;
  69.         
  70.         if (this->owningWindow)
  71.           this->theControl = 
  72.                   NewControl(this->owningWindow, itsBounds, itsText,
  73.                               visible, 0, 0, 1, procid, 13);
  74.         this->hasFrame = isFramed;
  75.         this->isEnabled = TRUE;
  76.         this->myValue = 0;
  77.         this->callBackProc = NULL;
  78.     }
  79.  
  80. /*-----------------------------------------------------------------*/
  81.  
  82.     CPPControl::~CPPControl (void)
  83.     {
  84.         if (this->theControl)
  85.           DisposeControl(this->theControl);
  86.     }
  87.  
  88. /*-----------------------------------------------------------------*/
  89.  
  90.     Boolean    CPPControl::Member (char *className)
  91.     {
  92.         if (strcmp(className, CPPControl::ClassName()) == 0)
  93.           return TRUE;
  94.         else
  95.           return CPPVisualObject::Member(className);
  96.     }
  97.     
  98. /*-----------------------------------------------------------------*/
  99.  
  100.     char    *CPPControl::ClassName (void)
  101.     {
  102.         return "CPPControl";
  103.     }
  104.  
  105. /*-----------------------------------------------------------------*/
  106.  
  107.     Boolean    CPPControl::DoClick (EventRecord *theEvent)
  108.     /* handle a click in the control - if visible */
  109.     {
  110.         Point    myPt = theEvent->where;
  111.         
  112.         if (this->theControl && this->IsVisible() && this->isEnabled)
  113.           {
  114.               Global2Local (&myPt);
  115.                if (TrackControl(this->theControl, myPt, this->callBackProc))
  116.                  DoOnClick ();
  117.                return TRUE;
  118.           }
  119.         
  120.         return FALSE;
  121.     }
  122.  
  123. /*-----------------------------------------------------------------*/
  124.  
  125.     void    CPPControl::Activate (Boolean nowActive)
  126.     /* activate/deactivate our control */
  127.     /* SUBCLASS SHOULD OVERRIDE */
  128.     {
  129.         // controls do not usually change their appearance
  130.         // when in background or foreground
  131.         
  132.         CPPVisualObject::Activate(nowActive);
  133.     }
  134.  
  135. /*-----------------------------------------------------------------*/
  136.  
  137.     void    CPPControl::MakeVisible (Boolean nowVisible)
  138.     /* hide/show our control */
  139.     /* IS OVERRIDE */
  140.     {
  141.         if (this->theControl)
  142.           {
  143.               if (nowVisible)
  144.                 ShowControl (this->theControl);
  145.               else
  146.                 HideControl (this->theControl);
  147.           }
  148.           
  149.         CPPVisualObject::MakeVisible(nowVisible);
  150.     }
  151.     
  152. /*-----------------------------------------------------------------*/
  153.  
  154.     void    CPPControl::Draw ()
  155.     /* is override */
  156.     {
  157.         PenState    OldState;
  158.         Rect        frameRect = *GetBounds();
  159.         
  160.         if (this->IsVisible() && this->theControl)
  161.           {
  162.               Draw1Control (theControl);
  163.               if (this->hasFrame)
  164.                 FrameControl(GetCVariant(this->theControl) == pushButProc);
  165.           }
  166.     }
  167.  
  168. /*-----------------------------------------------------------------*/
  169.  
  170.     void    CPPControl::FrameControl (Boolean useRoundRect)
  171.     /* draw the standard 3-pixel wide border around the control */
  172.     {
  173.         PenState    OldState;
  174.         Rect        frameRect = *GetBounds();
  175.             
  176.             GetPenState(&OldState);
  177.             InsetRect(&frameRect, -4, -4);
  178.             PenPat(black);
  179.             PenSize (3,3);
  180.             if (useRoundRect)
  181.               FrameRoundRect(&frameRect, 16, 16);
  182.             else
  183.               FrameRect (&frameRect);
  184.             SetPenState(&OldState);
  185.     }
  186.  
  187. /*-----------------------------------------------------------------*/
  188.  
  189.     void    CPPControl::SetValue (short newValue)
  190.     /* set the control's value to the value passed to us */
  191.     /* SUBCLASS MAY OVERRIDE */
  192.     {
  193.         SetCtlValue (this->theControl, this->myValue = newValue);
  194.     }
  195.     
  196. /*-----------------------------------------------------------------*/
  197.  
  198.     short    CPPControl::GetValue (void)
  199.     /* return the value of the control */
  200.     {
  201.         return this->myValue;
  202.     }
  203.  
  204. /*-----------------------------------------------------------------*/
  205.  
  206.     void    CPPControl::SetCallBack (ProcPtr callBack)
  207.     {
  208.         this->callBackProc = callBack;
  209.     }
  210.  
  211. /*-----------------------------------------------------------------*/
  212.  
  213.     Boolean    CPPControl::IsEnabled (void)
  214.     {
  215.         return this->isEnabled;
  216.     }
  217.     
  218. /*-----------------------------------------------------------------*/
  219.  
  220.     void    CPPControl::DoOnClick (void)
  221.     /* perform some action when the control is clicked */
  222.     /* SUBCLASS SHOULD OVERRIDE */
  223.     {
  224.         if (this->doClickProc)    // call the user-specified click handler
  225.          (doClickProc)(this->owningWObject);
  226.         else
  227.         if (this->commandEquivalent != kNoCommand)
  228.           this->owningWObject->DoCommand (this->commandEquivalent);
  229.     }
  230.  
  231. /*-----------------------------------------------------------------*/
  232.  
  233.     void    CPPControl::EnableControl (Boolean nowEnabled)
  234.     {
  235.         if (this->theControl)
  236.           {
  237.               if (nowEnabled != this->isEnabled)
  238.                 {
  239.                   InvalRect (GetBounds());
  240.                   this->isEnabled = nowEnabled;
  241.                   HiliteControl (this->theControl, (nowEnabled) ? 0 : 255);
  242.                 }
  243.           }
  244.     }
  245.  
  246. /*-----------------------------------------------------------------*/
  247.  
  248.     Rect    *CPPControl::GetBounds (void)
  249.     /* return the boundsrect of the object */
  250.     /* SUBCLASS MUST OVERRIDE */
  251.     {
  252.         if (this->theControl && this->IsVisible())
  253.           this->bounds = (*this->theControl)->contrlRect;
  254.         else
  255.           this->bounds = kEmptyRect;
  256.         
  257.         return &this->bounds;
  258.     }
  259.  
  260. /*-----------------------------------------------------------------*/
  261. /*---------------------- PROTECTED METHODS ------------------------*/
  262. /*-----------------------------------------------------------------*/
  263.  
  264.     void    CPPControl::MoveContent (short newH, short newV)
  265.     /* move the entire button to a new position */
  266.     {
  267.         if (this->theControl)
  268.           MoveControl (this->theControl, newH, newV);
  269.     }
  270.  
  271. /*-----------------------------------------------------------------*/
  272.  
  273.     void    CPPControl::ResizeContent (short newWidth, short newHeight)
  274.     /* change the size of the button */
  275.     {
  276.         if (this->theControl)
  277.           SizeControl(this->theControl, newWidth, newHeight);
  278.     }
  279.  
  280.  
  281.